<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/19
 * Time: 10:49
 */
namespace app\common\controller;

use app\common\lib\AdminAuth;
use app\common\lib\AdminMenu;
use app\common\model\BaseModel;
use liliuwei\think\Jump;
use think\App;
use think\facade\View;

class BackendController extends BaseController
{
    use Jump;
    /**
     * 无需登录的方法
     * @var array
     */
    protected $noNeedLogin = [];
    /**
     * 无需鉴权的方法
     * @var array
     */
    protected $noNeedCheck = [];
    /**
     * 后台auth实例
     * @var AdminAuth
     */
    protected $auth;
    /**
     * 当前查询模型
     * @var BaseModel
     */
    protected $model;
    /**
     * 后台菜单实例
     * @var AdminMenu
     */
    protected $menu;
    /**
     * 菜单树
     * @var
     */
    protected $menuTree;
    /**
     * 菜单视图（后台左侧菜单）
     * @var
     */
    protected $menuView;
    /**
     * 当前菜单信息
     * @var
     */
    protected $currentMenu;
    /**
     * 当前菜单对应的标题
     * @var
     */
    protected $title;
    /**
     * 面包屑
     * @var
     */
    protected $breadcrumb;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    protected function initialize()
    {
        parent::initialize();
        $this->auth = AdminAuth::instance();
        $this->app->bind('admin_auth',$this->auth);
        /**
         * 获取当前uri
         */
        $path = $this->uri;
        //检查是否需要登陆
        if(!$this->auth->match($this->noNeedLogin)){
            //检查是否登录
            if(!$this->auth->isLogin()){
                $this->redirect(url('login/login',['redirect'=>urlencode($this->request->url(true))]));
            }
            View::assign('admin_info',$this->auth->getAdmin());
            // 判断是否需要验证权限
            if(!$this->auth->match($this->noNeedCheck)){
                // 判断控制器和方法判断是否有对应权限
                if(!$this->auth->authCheck($path)){
                    $this->error('您没有权限访问此页面');
                }
            }
        }
        $this->menu = AdminMenu::instance($this->auth->getMenuList(),$this->uri);
        $this->currentMenu = $this->menu->getCurrentMenu();
        $this->title = empty($this->currentMenu) ? '' : $this->currentMenu['name'];
        $this->menuTree = $this->menu->getMenuTree();
        $this->menuView = $this->menu->menuToView($this->menuTree);
        $this->breadcrumb = $this->menu->getBreadCrumbView();
        View::assign('menu_view',$this->menuView);
        View::assign('breadcrumb',$this->breadcrumb);
        View::assign('title',$this->title);
        View::assign('site_title',config('config.site_title'));
        View::assign('site_keywords',config('config.site_keywords'));
        View::assign('site_description',config('config.site_description'));
        View::assign('site_icp',config('config.site_icp'));
    }
}